package sufutian.com.google_speech;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import sufutian.com.ibm_waston.KDBean;
import sufutian.com.ibm_waston.WatsonSpeech;
import sufutian.com.ibm_waston.WatsonSpeechTwo;
import sufutian.com.paypal.BrainTreeDemoActivity;
import sufutian.com.paypal.PayPalActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button long_speech, short_speech, watson_speech, watson_speech_nosession, pay_pal, braintree;
    private WatsonSpeech watsonSpeech;

    EditText edit_query;

    ArrayList<KDBean> lyricList = new ArrayList<>();

    private ListView listView;
    private LyricAdapter adapter;
    private WatsonSpeechTwo speechTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        long_speech = (Button) findViewById(R.id.long_speech);
        short_speech = (Button) findViewById(R.id.short_speech);
        watson_speech = (Button) findViewById(R.id.watson_speech);
        watson_speech_nosession = (Button) findViewById(R.id.watson_speech_nosession);
        braintree = (Button) findViewById(R.id.braintree);
        edit_query = (EditText) findViewById(R.id.edit_query);
        pay_pal = (Button) findViewById(R.id.pay_pal);
        listView = (ListView) findViewById(R.id.lyric_lv);
        adapter = new LyricAdapter();
        listView.setAdapter(adapter);

        long_speech.setOnClickListener(this);
        short_speech.setOnClickListener(this);
        watson_speech.setOnClickListener(this);
        watson_speech_nosession.setOnClickListener(this);
        pay_pal.setOnClickListener(this);
        braintree.setOnClickListener(this);

        watsonSpeech = new WatsonSpeech(this);
        speechTwo = new WatsonSpeechTwo(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.long_speech:

                break;
            case R.id.short_speech:

                break;
            case R.id.watson_speech:
                String filePath = Environment.getExternalStorageDirectory() + "/sufutian" + "/" + edit_query.getText();
                watsonSpeech.startRecognize(filePath, true, new WatsonSpeech.OnparseLisenter() {
                    @Override
                    public void onSuccess(ArrayList<KDBean> list) {

                    }
                });

                break;
            case R.id.watson_speech_nosession:
                String filePath2 = Environment.getExternalStorageDirectory() + "/sufutian" + "/" + edit_query.getText();
                watsonSpeech.startRecognize(filePath2, false, new WatsonSpeech.OnparseLisenter() {
                    @Override
                    public void onSuccess(ArrayList<KDBean> list) {
                        lyricList.clear();
                        lyricList.addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                });

//                String filePath3 = Environment.getExternalStorageDirectory() + "/sufutian" + "/" + "1499045617813.wav";
//                speechTwo.startRecognize(filePath3, false, new WatsonSpeechTwo.OnparseLisenter() {
//                    @Override
//                    public void onSuccess(ArrayList<KDBean> list) {
//                        Toast.makeText(MainActivity.this, "第二个转写成功", Toast.LENGTH_SHORT).show();
//                    }
//                });
                break;
            case R.id.pay_pal:
                Intent intent = new Intent(MainActivity.this, PayPalActivity.class);
//                Intent intent = new Intent(MainActivity.this, PayAuthoPalActivity.class);
                startActivity(intent);
                break;

            case R.id.braintree:
                Intent intent2 = new Intent(MainActivity.this, BrainTreeDemoActivity.class);
                startActivity(intent2);
                break;
        }
    }


    private class LyricAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return lyricList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(MainActivity.this, R.layout.lyric_item, null);
                holder = new ViewHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.lyric_tv);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (lyricList != null && position < lyricList.size()) {
                KDBean kdBean = lyricList.get(position);
                holder.textView.setText(kdBean.getStart() + "--" + kdBean.getLrc() + "--" + kdBean.getEnd());
            }
            return convertView;
        }


        private class ViewHolder {
            TextView textView;
        }


    }

}
